﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Infrastructure
{
    public partial class Folders : UserControl
    {
        private List<string> _folders;

        public string[] SelectedFolders
        {
            get
            {
                return _folders.ToArray();
            }
            set
            {
                _listBoxFolders.Items.Clear();
                _listBoxFolders.Items.AddRange( value );
            }
        }

        public Folders ()
        {

            InitializeComponent();
            _folders = new List<string>();
        }

        private void ButtonAddClick ( object sender, EventArgs e )
        {
            if (!string.IsNullOrEmpty( _textBoxPath.Text ) && Directory.Exists( _textBoxPath.Text ))
            {
                AddDirectory( _textBoxPath.Text );
                _textBoxPath.Text = string.Empty;
            }
            else if (_folderBrowserDialog.ShowDialog( this ) == DialogResult.OK)
            {
                AddDirectory( _folderBrowserDialog.SelectedPath );
            }
        }

        private void AddDirectory ( string directory )
        {
            foreach (var monitoredFolder in _folders)
            {
                if (directory == monitoredFolder)
                {
                    MessageBox.Show( string.Format( "Cannot add direcotry: {0}. \n\rDhe directory already exists", directory ) );
                    return;
                }
                else if (monitoredFolder.StartsWith( directory ))
                {
                    MessageBox.Show( string.Format( "Cannot add direcotry: {0}. \n\rDirectory is parent for: {1}", directory, monitoredFolder ) );
                    return;
                }
                else if (directory.StartsWith( monitoredFolder ))
                {
                    MessageBox.Show( string.Format( "Cannot add direcotry: {0}. \n\rDirectory is nested to {1}", directory, monitoredFolder ) );
                    return;
                }
            }
            _folders.Add( directory );
            _listBoxFolders.Items.Add( directory );
        }

        private void ButtonRemoveClick ( object sender, EventArgs e )
        {
            if (_listBoxFolders.SelectedItem != null)
            {
                _folders.Remove( _listBoxFolders.SelectedItem.ToString() );
                _listBoxFolders.Items.Remove( _listBoxFolders.SelectedItem );
                _textBoxPath.Clear();
            }
        }

        private void ListBoxAddSelectedIndexChanged ( object sender, EventArgs e )
        {
            if (_listBoxFolders.SelectedItem != null)
            {
                _buttonRemove.Enabled = true;
                _textBoxPath.Text = _listBoxFolders.SelectedItem.ToString();
            }
            else
            {
                _buttonRemove.Enabled = false;
            }
        }
    }
}
